package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ICompanyBalanceDAO;
import org.jsola.hr.dto.CompanyBalanceAddDTO;
import org.jsola.hr.dto.CompanyBalanceUpdateDTO;
import org.jsola.hr.entity.CompanyBalanceDO;
import org.jsola.hr.query.CompanyBalanceQuery;
import org.jsola.hr.service.ICompanyBalanceService;
import org.jsola.hr.vo.CompanyBalanceListVO;
import org.jsola.hr.vo.CompanyBalanceVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用工单位账面余额表
 *
 * @author zhr
 */
@Service("hrCompanyBalanceServiceImpl")
public class CompanyBalanceServiceImpl implements ICompanyBalanceService {

    @Autowired
    private ICompanyBalanceDAO companyBalanceDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public CompanyBalanceVO save(CompanyBalanceAddDTO companyBalanceAddDTO, TokenUser tokenUser) {
        // 转DO
        CompanyBalanceDO companyBalanceDO = companyBalanceAddDTO.to(CompanyBalanceDO.class);
        // 保存
        companyBalanceDO = save(companyBalanceDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return companyBalanceDO.to(CompanyBalanceVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(CompanyBalanceUpdateDTO companyBalanceUpdateDTO, TokenUser tokenUser) {
        //转DO
        CompanyBalanceDO companyBalanceDO = companyBalanceUpdateDTO.to(CompanyBalanceDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(companyBalanceDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... companyBalanceIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) companyBalanceIds);
    }


    @Override
    public CompanyBalanceVO selectById(Long companyBalanceId, String siteId) {
        CompanyBalanceDO companyBalanceDO = selectDOById(companyBalanceId, siteId);
        if (companyBalanceDO == null) {
            return null;
        }
        return companyBalanceDO.to(CompanyBalanceVO.class);
    }

    @Override
    public CompanyBalanceVO selectBySiteId(String siteId) {
        Example example = new Example(CompanyBalanceDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        CompanyBalanceDO companyBalanceDO = companyBalanceDAO.selectOneByExample(example);
        if (companyBalanceDO == null) {
            return null;
        }
        return companyBalanceDO.to(CompanyBalanceVO.class);
    }

    @Override
    public CompanyBalanceVO selectByCompanyIds(String[] companyIds, String siteId) {
        CompanyBalanceVO balanceVO = new CompanyBalanceVO();
        Example example = new Example(CompanyBalanceDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        List<String> companyIdList = Arrays.asList(companyIds);
        if (!CollectionUtils.isEmpty(companyIdList)) {
            example.and()
                    .andIn("companyId", companyIdList);
        }
        List<CompanyBalanceDO> companyBalanceDOList = companyBalanceDAO.selectByExample(example);
        BigDecimal amount = BigDecimal.ZERO;
        List<Long> ids = new ArrayList<>();
        // 计算总金额
        for (CompanyBalanceDO companyBalanceDO : companyBalanceDOList) {
            amount = amount.add(companyBalanceDO.getAmount());
            ids.add(companyBalanceDO.getId());
        }
        balanceVO.setIds(ids);
        balanceVO.setAmount(amount);
        balanceVO.setCompanyIds(companyIdList);
        return balanceVO;
    }

    @Override
    public List<CompanyBalanceListVO> select(CompanyBalanceQuery companyBalanceQuery, String siteId) {
        List<CompanyBalanceDO> companyBalanceDOList = selectDO(companyBalanceQuery, siteId);
        if (CollectionUtils.isEmpty(companyBalanceDOList)) {
            return companyBalanceDOList == null ? null : new ArrayList<>();
        }
        return companyBalanceDOList.stream()
                .map(companyBalanceDO -> companyBalanceDO.to(CompanyBalanceListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CompanyBalanceQuery companyBalanceQuery, String siteId) {
        Example example = buildExample(companyBalanceQuery, siteId);
        return companyBalanceDAO.selectCountByExample(example);
    }

    @Override
    public Page<CompanyBalanceListVO> selectPage(CompanyBalanceQuery companyBalanceQuery, String siteId) {
        Example example = buildExample(companyBalanceQuery, siteId);
        Page<CompanyBalanceDO> page = companyBalanceDAO.selectPageByExample(example,
                companyBalanceQuery.getPageNo(),
                companyBalanceQuery.getPageSize());

        return page.to(CompanyBalanceListVO.class);
    }

    @Override
    public CompanyBalanceDO selectDOById(Long companyBalanceId, String siteId) {
        return listById(companyBalanceId, siteId);
    }

    @Override
    public List<CompanyBalanceDO> selectDO(CompanyBalanceQuery companyBalanceQuery, String siteId) {
        Example example = buildExample(companyBalanceQuery, siteId);
        return companyBalanceDAO.selectByExample(example);
    }

    @Override
    public CompanyBalanceDO selectByCompanyId(String companyId, String siteId) {
        Example example = new Example(CompanyBalanceDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId)
                .andEqualTo("companyId", companyId);

        return companyBalanceDAO.selectOneByExample(example);
    }

    @Override
    public List<CompanyBalanceDO> selectAll() {
        Example example = new Example(CompanyBalanceDO.class);
        example.and()
                .andEqualTo("valid", true);
        return companyBalanceDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param companyBalanceQuery 查询参数
     * @param siteId              所属站点id
     * @return example
     */
    private Example buildExample(CompanyBalanceQuery companyBalanceQuery, String siteId) {
        Example example = new Example(CompanyBalanceDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (companyBalanceQuery.getGmtCreateStart() != null &&
                companyBalanceQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", companyBalanceQuery.getGmtCreateStart(),
                    companyBalanceQuery.getGmtCreateEnd());
        }
        // 排序
        ExampleKit.setExampleOrder(example, companyBalanceQuery.getOrders());
        return example;
    }
}




